<?php

    define('CACHEDIR', dirname(__FILE__).'/cache/');
    include('functions.php');
    
    $isCached = false;
    if(file_exists(CACHEDIR.'statefor2time') && file_exists(CACHEDIR.'statefor2data')){
        $statefor2time = readCache('statefor2time');
        if($statefor2time+600 > time()) $isCached = true;
    }

    if(!$isCached) {
        $str = curl('http://sec.djmaxcn.com/djmax_technika2/location.asp');
        require('simple_html_dom.php');
        $html = new simple_html_dom();
        $html->load($str);
        $table = array();
        
        foreach($html->find('.info-title') as $tr) {
            $tmp = array();
            $i = 1;
            foreach($tr->find('td') as $td) {
                if($i++%4) {
                    $tmp[] = $td->innertext;
                } else {
                    $tmp[] = preg_match('/icon_net_off/i', $td->innertext)?'Off':'On';
                }
            }
            $table[] = $tmp;
        }
        
        writeCache('statefor2time', time());
        writeCache('statefor2data', $table);
    } else {
        $table = readCache('statefor2data');
    }

    require 'smarty/Smarty.class.php';
    $smarty = new Smarty;
    $smarty->assign('version', $statefor2time?$statefor2time:time());
    $smarty->assign('table', $table);
    $smarty->display('statefor2.tpl');